<template>
  <el-dialog
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="globalData.title"
    :visible.sync="isDialog.visible"
    top="5vh"
    width="1050px"
  >
    <div class="header-box"></div>

    <div class="flex-content">
      <el-table
        :data="globalData.orderItem"
        header-cell-class-name="table-header"
        height="100%"
        stripe
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="商品名称"
          prop="goods_name"
          width="250"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="商品规格"
          prop="spec_name"
          width="150"
        >
        </el-table-column>

        <el-table-column align="center" label="成交价" prop="final_price">
        </el-table-column>

        <el-table-column align="center" label="购买数量" prop="buyNum">
        </el-table-column>

        <el-table-column align="center" label="付款信息" prop="payInfo">
        </el-table-column>

        <el-table-column align="center" label="退单数量">
          <template slot-scope="{ row, $index }">
            <el-input
              v-model="submitForm.orderItem[$index].refundNum"
              :max="row.buyNum"
              :min="0"
              :setp="0.01"
              clearable
              placeholder="请输入退单数量"
              type="number"
            ></el-input>
          </template>
        </el-table-column>

        <el-table-column align="center" label="退单金额">
          <template slot-scope="{ row, $index }">
            <el-input
              v-model="submitForm.orderItem[$index].refundCash"
              :max="row.final_price"
              :min="0"
              :setp="0.01"
              clearable
              placeholder="请输入退单金额"
              type="number"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { refund } from "@/api/order";

export default {
  name: "Refund",
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        orderId: this.info.info.order_id,
        orderItem: this.info.orderItem.map((item) => ({
          orderItemId: item.orderItemId,
          refundNum: item.refundNum,
          refundCash: Number(item.refundAmount),
        })),
      },
      // 数据
      globalData: {
        title: "",
        orderItem: [],
      },
      // 弹窗
      isDialog: {
        visible: true,
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
    };
  },
  created() {
    this.globalData.title = "申请退款" + "(" + this.info.info.order_sn + ")";
    this.globalData.orderItem = this.info.orderItem;
  },
  methods: {
    // 提交
    handleSubmit() {
      this.isLoading.submit = true;

      refund(this.submitForm)
        .then(() => {
          this.isLoading.submit = false;
          this.$emit("close");
        })
        .catch(() => {
          this.isLoading.submit = false;
        });
    },
    // 关闭
    handleClose() {
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-table {
  min-height: 60vh;
}

::v-deep .el-dialog__body {
  padding: 20px 20px;
}

.header-box {
  display: flex;

  > div {
    margin-right: 10px;
  }
}

.flex-content {
  margin-top: 10px;
}
</style>
